package com.martin.ads.pano360demo;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.Matrix;

import com.martin.ads.vrlib.filters.base.AbsFilter;
import com.martin.ads.vrlib.filters.vr.AbsHotspot;
import com.martin.ads.vrlib.object.Plane;
import com.martin.ads.vrlib.programs.GLPassThroughProgram;
import com.martin.ads.vrlib.utils.TextureUtils;

import java.util.List;

/**
 * @author lejun
 * @Description:
 */
public class HotsportContainer extends AbsFilter {


    private GLPassThroughProgram glPassThroughProgram;
    private List<AbsHotspot> hotspotList;
    private Plane imagePlane;
    private float[] mMVPMatrix = new float[16];

    private Context context;

    public HotsportContainer(Context context) {
        this.context = context;
        glPassThroughProgram =new GLPassThroughProgram(context);
        Matrix.setIdentityM(mMVPMatrix, 0);
        imagePlane = new Plane(false);
    }

    public void setHotspotList(List<AbsHotspot> hotspotList) {
        this.hotspotList = hotspotList;
    }

    private void drawHotSpot(){
        for(AbsHotspot hotSpot: hotspotList){
            hotSpot.onDrawFrame(0);
        }
    }


    @Override
    public void init() {
        glPassThroughProgram.create();
        for(AbsHotspot hotSpot: hotspotList)
            hotSpot.init();

    }

    @Override
    public void destroy() {
        glPassThroughProgram.onDestroy();
        for(AbsHotspot hotSpot: hotspotList)
            hotSpot.destroy();
    }

    @Override
    public void onDrawFrame(int textureId) {
        glPassThroughProgram.use();
        imagePlane.uploadTexCoordinateBuffer(glPassThroughProgram.getTextureCoordinateHandle());
        imagePlane.uploadVerticesBuffer(glPassThroughProgram.getPositionHandle());
        GLES20.glUniformMatrix4fv(glPassThroughProgram.getMVPMatrixHandle(), 1, false, mMVPMatrix, 0);
        TextureUtils.bindTexture2D(textureId,GLES20.GL_TEXTURE0,glPassThroughProgram.getTextureSamplerHandle(),0);
        onPreDrawElements();
        imagePlane.draw();
        drawHotSpot();
    }

    @Override
    public void onFilterChanged(int width, int height) {
        super.onFilterChanged(width,height);
        for(AbsHotspot hotSpot: hotspotList)
            hotSpot.onFilterChanged(width,height);
    }
}
